<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="success" size="mini" icon="el-icon-refresh" @click="handleReload">采集器加载</el-button>
        <el-button type="danger" size="mini" icon="el-icon-close" @click="handleStop">采集器停止</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coldefinetableListData" >
        <!-- 添加序号列 -->
      <el-table-column label="序号" type="index" width="60" align="center">
        <template slot-scope="scope">
          <!-- 计算真实序号（考虑分页） -->
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="点位名称" align="center" prop="point" />
      <el-table-column label="ip" align="center" prop="ip" />
      <el-table-column label="位置" align="center" prop="area" />
      <el-table-column label="数值" align="center" prop="value" />
      <el-table-column label="记录时间" align="center" prop="time" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>

</template>

<script>
import { listColdefinetableData, reloadCollector, stopCollector} from "@/api/businessInfo/coldefinetableData";

export default {
  name: "Coldefinetable",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位表格数据
      coldefinetableListData: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        point: null,
        ip: null,
        area: null,
        value: null,
        time: null,
      },
      // 表单参数
      form: {}
      // 表单校验
    };
  },
  created() {
    this.getList();
  },
  methods: {
// 获取数据并实现本地分页
getList() {
      this.loading = true;
      listColdefinetableData(this.queryParams).then(response => {
        // 保存原始全量数据
        this.allData = response.data || [];
        // 更新总条数
        this.total = this.allData.length;
        // 计算当前页数据
        this.updatePageData();
        this.loading = false;
      }).catch(error => {
        this.loading = false;
        this.$message.error('获取数据失败');
      });
    },
    
    // 根据当前页码和每页条数计算显示的数据
    updatePageData() {
      const { pageNum, pageSize } = this.queryParams;
      const start = (pageNum - 1) * pageSize;
      const end = start + pageSize;
      
      // 从全量数据中截取当前页需要显示的数据
      this.coldefinetableListData = this.allData.slice(start, end);
    },
    
    // 处理页码变化
    handleCurrentChange(newPage) {
      this.queryParams.pageNum = newPage;
      this.updatePageData();
    },
    
    // 处理每页条数变化
    handleSizeChange(newSize) {
      this.queryParams.pageSize = newSize;
      this.queryParams.pageNum = 1; // 重置为第一页
      this.updatePageData();
    },

    /** 采集器加载 */
    handleReload() {
      this.$confirm('确认重新加载采集器吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        reloadCollector().then(res => {
          this.$message.success("采集器已加载！");
        });
      });
    },

    /** 采集器停止 */
    handleStop() {
      this.$confirm('确认停止采集器吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        stopCollector().then(res => {
          this.$message.success("采集器已停止！");
        });
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }

  }
};
</script>
